package org.csu.backofficemanagersys.controller;

import com.alibaba.fastjson.JSONObject;
import org.csu.backofficemanagersys.Response.ResponseMsg;
import org.csu.backofficemanagersys.domain.Account;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.service.AccountService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/mypetstore/account")
public class AccountController {
    private AccountService accountService;

    @Autowired
    private void setAccountService(AccountService accountService){this.accountService=accountService;}

    @GetMapping("/index")
    public void Account(){

    }
    @GetMapping("/table")
    public ResponseMsg<JSONObject> find(int currentPage, int size,String userid,String email,String phone){
        if(userid == null && email == null && phone ==null){
            return SelectOneByName(currentPage,size);
        }
        List<Account> accounts = accountService.findAccountLike(userid,email,phone);
        List<Account> page ;
        int total = accounts.size();
        if(total == 0){
            return ResponseMsg.createForSuccess("表格为空",null);
        }
        try {
            if( total<=size*currentPage-1 && total>=(currentPage-1)*size ){
                page = accounts.subList(size * (currentPage - 1), total);
            }else {
                page = accounts.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("total",total);
            jsonObject.put("list",page);
            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }
    }


    @PutMapping("/table")
    public ResponseMsg<Objects> UpdateAccount(@ModelAttribute Account account){
       if(accountService.updateAccount(account)){
           return ResponseMsg.createForSuccess();
       }
       return ResponseMsg.createForError();
    }
    @PutMapping("/table/reset/{userid}")
    public ResponseMsg<Objects> ResetPassword(@PathVariable("userid") String username){
       if(accountService.ReSetPassword(username)){
           return ResponseMsg.createForSuccess();
       }
       return ResponseMsg.createForError();
    }
    @GetMapping("/exist/{userid}")
    public ResponseMsg<Objects> AccountExit(@PathVariable String userid){
       if(accountService.getAccountByUsername(userid)!=null){
           return ResponseMsg.createForSuccessMsg("用户存在");
       }
       return ResponseMsg.createForSuccessMsg("用户不存在");
    }

    public ResponseMsg<JSONObject> SelectOneByName(int currentPage, int size){
        try {

            List<Account> accountList = accountService.getAllAccount();
            JSONObject jsonObject = new JSONObject();
            List<Account> accounts;
            if(accountList.size()<size*currentPage && accountList.size()>(currentPage-1)*size){
                accounts = accountList.subList(size * (currentPage - 1), accountList.size());
            }else {
                accounts = accountList.subList(size * (currentPage - 1), size * currentPage - 1);
            }
            jsonObject.put("total",accountList.size());
            jsonObject.put("list",accounts);

            return ResponseMsg.createForSuccess("获取表格元素成功",jsonObject);
        }catch (Exception e){
            return ResponseMsg.createForError("获取表格元素失败");
        }

    }
}
